package com.aruba.screenlibrary;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.view.WindowManager;

import java.lang.reflect.Field;

/**
 * 获取缩放后的宽高
 */
public class UIUtils {
    private static UIUtils instance;

    public static UIUtils getInstance() {
        if (instance == null) {
            instance = new UIUtils();
        }
        return instance;
    }

    //ui图上的宽高
    private static final int BASE_WIDTH = 1080;
    private static final int BASE_HEIGHT = 1920; // ui给的图包不含状态栏，后面要减去状态栏的高度

    //设备的真实宽高
    private static int displayWidth;
    private static int displayHeight; // 后面要减去状态栏的高度
    //状态栏高度
    private int statusBarHeight;
    //屏幕信息
    private DisplayMetrics displayMetrics;
    private boolean isInit;

    //初始化真实宽高
    public void init(Context context) {
        init(context, true);
    }

    //初始化真实宽高
    public void init(Context context, boolean ignoreScreenOrientation) {
        //使用WindowManager获取
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);

        //获取状态栏高度
        statusBarHeight = getStatusBarHeight(context);
        //根据状态栏高度初始化真实宽高
        refreshDisplayMetrics(ignoreScreenOrientation);

        isInit = true;
    }

    public boolean isInit() {
        return isInit;
    }

    public void registerActivity(Activity activity) {
        ViewsParseUtil.parseAcitvity(activity);
    }

    public void registerViewGroup(final ViewGroup viewGroup) {
        viewGroup.post(new Runnable() {
            @Override
            public void run() {
                ViewsParseUtil.catchViewGroup(viewGroup);
            }
        });
    }

    /**
     * 设置真实长宽
     *
     * @param ignoreScreenOrientation
     */
    public void refreshDisplayMetrics(boolean ignoreScreenOrientation) {
        //判断横竖屏
        if (!ignoreScreenOrientation && displayMetrics.widthPixels > displayMetrics.heightPixels) {//横屏
            displayWidth = displayMetrics.heightPixels;
            displayHeight = displayMetrics.widthPixels;
        } else { //竖屏
            displayWidth = displayMetrics.widthPixels;
            displayHeight = displayMetrics.heightPixels;
        }
    }

    //使用运行过程中的资源文件
    private int getStatusBarHeight(Context context) {
        int statusBarHeight = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (statusBarHeight > 0) {
            return statusBarHeight;
        }

        //上面方法获取不到的话，使用反射资源获取
        return getStatusBarHeightByAndroid(context, "com.android.internal.R$dimen", "system_bar_height", 48);
    }

    private int getStatusBarHeightByAndroid(Context context, String className, String fieldName, int defValue) {
        try {
            //获取class
            Class aClass = Class.forName(className);
            //获取实例
            Object o = aClass.newInstance();
            Field field = aClass.getField(fieldName);
            int id = Integer.parseInt(field.get(o).toString());
            return context.getResources().getDimensionPixelOffset(id);
        } catch (Exception e) {
            return defValue;
        }
    }

    public float getWidth(float width) {
        return width * displayWidth / BASE_WIDTH;
    }

    public float getHeight(float height) {
        return height * (displayHeight - statusBarHeight) / (BASE_HEIGHT - statusBarHeight);
    }

    public int getDisplayWidth() {
        return displayWidth;
    }

    public int getDisplayHeight() {
        return displayHeight;
    }
}
